package com.csun.cmny.provider.web.frontend;

import com.csun.cmny.core.support.BaseController;
import com.csun.cmny.provider.model.domain.OpcAppClock;
import com.csun.cmny.provider.model.dto.AppClockQueryDto;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.OpcAppClockService;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2019-10-28 21:08
 * @Description:
 */
@RestController
@RequestMapping(value = "/appClock", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class OpcAppClockController extends BaseController {

    @Resource
    private OpcAppClockService opcAppClockService;
    @Resource
    private CmnyMessageSource cmnyMessageSource;

    /**
     * 保存
     */
    @PostMapping("/save")
    public Wrapper save(@RequestBody OpcAppClock opcAppClock) {

        Integer result = opcAppClockService.save(opcAppClock, getLoginAuthDto());

        //return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null), result);
    }

    /**
     * 删除
     */
    @PostMapping("/deleteById/{id}")
    public Wrapper deleteById(@PathVariable Long id) {

        Integer result = opcAppClockService.deleteByKey(id);

        //return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null), result);
    }

    /**
     * 查询
     */
    @PostMapping("/queryListWithPage")
    public Wrapper queryListWithPage(@RequestBody AppClockQueryDto appClockQueryDto) {

        appClockQueryDto.setUserId(getLoginAuthDto().getUserId());

        PageHelper.startPage(appClockQueryDto.getPageNum(), appClockQueryDto.getPageSize());

        List<OpcAppClock> list = opcAppClockService.queryListWithPage(appClockQueryDto);

        //return WrapMapper.ok(new PageInfo<>(list));
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null), new PageInfo<>(list));
    }
}
